package com.young.config;

import com.alibaba.fastjson.JSONArray;
import com.young.common.AdminConstant;
import com.young.domain.SysUser;
import com.young.res.vo.AuthVo;
import com.young.shop.service.SysUserService;
import java.util.ArrayList;
import java.util.List;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @Description:
 * @Author: kangyang
 * @Date: 2021/5/8 11:18
 **/
@Configuration
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Author kangyang
     * @Description 授权 n次
     * @Date 11:19 2021/5/8
     * @Param [principalCollection]
     * @return org.apache.shiro.authz.AuthorizationInfo
     **/
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SysUser user = (SysUser) principalCollection.getPrimaryPrincipal();
        List<String> perms = new ArrayList<>();
        Object o = redisTemplate.opsForValue()
            .get(AdminConstant.AUTH_PERMISSION_PRE + user.getUserId());
        if(o != null) {
            String jsonStr = String.valueOf(o);
            List<AuthVo> authVos = JSONArray.parseArray(jsonStr, AuthVo.class);
            authVos.forEach(authVo -> {
                perms.add(authVo.getAuthority());
            });
        }
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(perms);
        return simpleAuthorizationInfo;
    }

    /**
     * @Author kangyang
     * @Description 登录 1次
     * @Date 11:20 2021/5/8
     * @Param [authenticationToken]
     * @return org.apache.shiro.authc.AuthenticationInfo
     **/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) {
        String username = authenticationToken.getPrincipal().toString();
        SysUser user = sysUserService.getUserByUserName(username);
        if(user == null) {
            throw new AccountException("账户不存在");
        }
        ByteSource salt = ByteSource.Util.bytes("young".getBytes());
        return new SimpleAuthenticationInfo(user, user.getPassword(), salt, username);
    }


    public static void main(String[] args) {
        Md5Hash md5Hash = new Md5Hash("123456", ByteSource.Util.bytes("young".getBytes()), 2);
        String hex = md5Hash.toHex();
        System.out.println(hex);
    }
}
